package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangtai.modules.dto.CredentialInfoDto;
import com.zhangtai.modules.entity.credential.CredentialInfoEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-15 15:22:38
 */
@Mapper
public interface CredentialInfoDao extends BaseMapper<CredentialInfoEntity> {

    @Select("select t1.*,t1.unit_id company_id,t2.name,t2.idcard,t2.gender,t2.birthday,t2.job_id,t2.org_id,t2.unit_id,t3.allowance,t3.allowance_time,t3.reason hang_reason, t3.bpm_url hang_bpm_url," +
            "t3.file_url hang_file_url,t3.id hang_id from tb_credential_info t1 " +
            "left join tb_employee_info t2 on t1.oid = t2.oid " +
            "left join (select * from tb_credential_hang where ((STATUS !=3  AND STATUS !=4 AND STATUS !=5 AND STATUS IS NOT NULL) OR status IS NULL))t3 on t1.id = t3.credential_id " +
            "where t1.oid = #{oid} and t1.is_delete = 0   " +
            "order by create_time desc ")
    List<CredentialInfoDto> getCredentialInfoList(@Param("oid") Long oid, IPage<CredentialInfoDto> page);

    @Select("<script>select t1.*,t1.unit_id company_id,t2.name,t2.idcard,t2.gender,t2.birthday,t2.job_id,t2.org_id,t2.unit_id unitId1,t3.allowance,t3.allowance_time,t3.reason hang_reason," +
            "t3.file_url hang_file_url,t3.bpm_url  from tb_credential_info t1 " +
            "left join tb_employee_info t2 on t1.oid = t2.oid " +
            "left join (select * from tb_credential_hang where ((STATUS !=3  AND STATUS !=4 AND STATUS !=5 AND  STATUS IS NOT NULL) OR status IS NULL)) t3 on t1.id = t3.credential_id " +
            "<where>  t1.is_delete = 0 " +
            "<if test='name!=null and name!=&apos;&apos;'> and name like '%${name}%' </if>" +
            "<if test='status!=null and status.size>0'> and t1.status in " +
            " <foreach item=\"s\" index=\"index\" collection=\"status\" open=\"(\" separator=\",\" close=\")\">#{s}</foreach>" +
            "</if>"+
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+
            "<if test='orgIds==null or orgIds.size==0'> and t1.unit_id = null " +

            "</if>"+
            "</where>" +
            "order by create_time desc </script>")
    List<CredentialInfoDto> getCredentialsInfoList(@Param("name") String name,
                                                  @Param("status") List<Integer> status,
                                                  @Param("orgIds") List<Long> orgIds,
                                                  IPage<CredentialInfoDto> page);

    @Update("update tb_credential_info set is_audit = 0 where end_time < #{time}")
    void updateCredentialPast(@Param("time") String time);
}
